package com.xfgg.cloud.api;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * threadPoolKey：定义用于处理请求的线程池标识符
 * coreSize：主要线程数
 * maxQueueSize：等待队列中的最大任务数
 * queueSizeRejectionThreshold：队列拒绝阈值
 * execution.isolation.thread.timeoutInMilliseconds：执行线程的超时时间
 * circuitBreaker.errorThresholdPercentage：断路器的错误阈值百分比，
 * circuitBreaker.sleepWindowInMilliseconds：断路器的休眠时间窗口
 *
 * @author xfgg
 */
@Service
@DefaultProperties(
        threadPoolKey = "GetSearchApi",
        threadPoolProperties = {
                @HystrixProperty(name = "coreSize", value = "20"),
                @HystrixProperty(name = "maxQueueSize", value = "120"),
                @HystrixProperty(name = "queueSizeRejectionThreshold", value = "500")},
        commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "80"),
                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "20"),
                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "20000")})
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SearchApiService {
    private final GetSearchApi getSearchApi;

    @HystrixCommand(fallbackMethod = "getSearchCallback")
    public String getSearch() {
        return getSearchApi.getSearchTerm();
    }

    public String getSearchCallback() {
        return "";
    }
}
